package com.example.secondexam;

import android.os.Bundle;
import android.view.View;
import android.view.Gravity;
import android.widget.LinearLayout;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private LinearLayout containerLinearLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        containerLinearLayout = findViewById(R.id.containerLinearLayout);

        findViewById(R.id.btnHorizontal).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                containerLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
            }
        });

        findViewById(R.id.btnVertical).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                containerLinearLayout.setOrientation(LinearLayout.VERTICAL);
            }
        });

        findViewById(R.id.btnLeft).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 设置布局为垂直方向，并设置子View的布局参数为靠左
                containerLinearLayout.setOrientation(LinearLayout.VERTICAL);
                for (int i = 0; i < containerLinearLayout.getChildCount(); i++) {
                    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) containerLinearLayout.getChildAt(i).getLayoutParams();
                    params.gravity = Gravity.LEFT;
                    containerLinearLayout.getChildAt(i).setLayoutParams(params);
                }
            }
        });
    }

    @Override
    public void onBackPressed() {
        // 这里可以添加返回主界面的逻辑
        super.onBackPressed();
    }
}
